// pages/joke/joke.ts
interface JakeData<T> {
  code: number;
  data: T;
  msg: string;
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    jakeList: [] as string[],
    isRefReshing: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getJokeList().then((res) => {
      // console.log('aaa',res);
      this.setData({
        jakeList: res,
      });
    });
  },

  getJokeList() {
    return new Promise<string[]>((resolve, reject) => {
      wx.showLoading({ title: "拼命加载中..." });
      wx.request<JakeData<string[]>>({
        method: "GET",
        url: "https://api-vue-base.itheima.net/api/joke/list",
        data: {
          num: 10,
        },
        success: (res) => {
          // console.log(res);
          // this.setData({
          //   jakeList: res.data.data,
          // });
          resolve(res.data.data);
        },
        fail: () => {
          reject();
        },
        complete: () => {
          wx.hideLoading();
        },
      });
    });
  },

  // 下拉刷新
  async onRefresherrefresh() {
    this.setData({
      isRefReshing: true,
    });
    this.setData({
      jakeList: await this.getJokeList(),
    });
    this.setData({
      isRefReshing: false,
    });
  },

  // 触底加载，获取更多笑话
  async getMoreData() {
    wx.showToast({ title: "滚动触底" });
    const res = await this.getJokeList();
    this.setData({
      jakeList: [...this.data.jakeList, ...res],
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
